package com.fish.yssb.ui.page.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.fish.yssb.R;
import com.fish.yssb.app.BaseApp;
import com.fish.yssb.ui.page.util.TopToastUtil;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;


public class getDataByTimeDialog extends Dialog implements CompoundButton.OnCheckedChangeListener {

	/**
	 * 上下文对象 *
	 */
	Activity context;

	private Button btn_save_zq_u,btn_chancel_zq_u;

//	public EditText text_para;

	private TextView tv_starttime,tv_endtime;
	private LinearLayout ll_changetime;

	private String title,curval,changeval;
	private CheckBox ck_all;
	private TimePickerView pvTime;

	public getDataByTimeDialog(Activity context) {
		super(context);
		this.context = context;
	}



	private View.OnClickListener mClickListener;
	public getDataByTimeDialog(Activity context, View.OnClickListener clickListener, String title , String curval, String changeval) {
		super(context);
		this.context = context;
		this.mClickListener = clickListener;
		this.title = title;
		if(curval == null ||curval.equalsIgnoreCase(""))
		{
			curval = "暂无";
		}
		this.curval = curval;
		this.changeval = changeval;
	}

	private int type =1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 指定布局
		initTimePicker();
		isAll = false;
		this.setContentView(R.layout.getdata_time_dialog);

//		text_para = (EditText) findViewById(R.id.text_para);
//		tv_title = (TextView)findViewById(R.id.tv_title);

		ll_changetime = (LinearLayout)findViewById(R.id.ll_changetime);
		tv_starttime = (TextView)findViewById(R.id.tv_starttime);

		tv_endtime = (TextView)findViewById(R.id.tv_endtime);

		tv_starttime.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if(!ck_all.isChecked()) {
					type = 1;

					pvTime.show(v);
				}
			}
		});

		ck_all = (CheckBox)findViewById(R.id.ck_all);
		ck_all.setOnCheckedChangeListener(this);

		tv_endtime.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				if(!ck_all.isChecked()) {
					type = 2;
					pvTime.show(v);
				}
			}
		});


//		text_para.setText(changeval);
		/*
		 * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
		 * 对象,这样这可以以同样的方式改变这个Activity的属性.
		 */
		Window dialogWindow = this.getWindow();

		WindowManager m = context.getWindowManager();
		Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
		WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
		// p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
		p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.8
		dialogWindow.setAttributes(p);

		// 根据id在布局中找到控件对象
		btn_save_zq_u = (Button) findViewById(R.id.btn_save_zq_u);

		btn_chancel_zq_u = (Button)findViewById(R.id.btn_chancel_zq_u);
		// 为按钮绑定点击事件监听器
		btn_save_zq_u.setOnClickListener(mClickListener);
		btn_chancel_zq_u.setOnClickListener(mClickListener);

		this.setCancelable(true);
	}

	private void initTimePicker() {//Dialog 模式下，在底部弹出
		pvTime = new TimePickerBuilder(context, new OnTimeSelectListener() {
			@Override
			public void onTimeSelect(Date date, View v) {
//                Toast.makeText(context, getTime(date), Toast.LENGTH_SHORT).show();
				Log.i("pvTime", "onTimeSelect");

				if(type == 1)
				{
					tv_starttime.setText(BaseApp.getInstance().dateToString(date,"yyyy-MM-dd HH:mm:ss"));
				}
				else{
					tv_endtime.setText(BaseApp.getInstance().dateToString(date,"yyyy-MM-dd HH:mm:ss"));
				}

			}
		})
				.setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
					@Override
					public void onTimeSelectChanged(Date date) {
						Log.i("pvTime", "onTimeSelectChanged");

					}
				})
				.setTitleText("查询设置")
				.setType(new boolean[]{true, true, true, true, true, true})
				.isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
				.addOnCancelClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						Log.i("pvTime", "onCancelClickListener");
					}
				})
				.setItemVisibleCount(5) //若设置偶数，实际值会加1（比如设置6，则最大可见条目为7）
				.setLineSpacingMultiplier(2.0f)
				.isAlphaGradient(true)
				.build();

		Dialog mDialog = pvTime.getDialog();
		if (mDialog != null) {

			FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT,
					Gravity.BOTTOM);

			params.leftMargin = 0;
			params.rightMargin = 0;
			pvTime.getDialogContainerLayout().setLayoutParams(params);

			Window dialogWindow = mDialog.getWindow();
			if (dialogWindow != null) {
				dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
				dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
				dialogWindow.setDimAmount(0.3f);
			}
		}
	}

	private String getTime(Date date) {//可根据需要自行截取数据显示
		Log.d("getTime()", "choice date millis: " + date.getTime());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(date);
	}

	private Boolean isAll;
	@Override
	public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
		if(isChecked){                      //选项被选取
			isAll = true;
			ll_changetime.setVisibility(View.GONE);
		}else {                             //选项被取消
			isAll = false;
			ll_changetime.setVisibility(View.VISIBLE);
		}


	}

}


